% run.m
% =====
%
% Compute the similarity matrix for two input houses, and calculate the
% similarity of each sensor in the source domain with each one in the
% target.
%
% INPUT
%    HOUSE1: the first house, as it is saved in the Kasteren's dataset
%    HOUSE2: the second house, as it is saved in the Kasteren's dataset
%
% OUTPUT
%	GMM1: array of the sensor profiles (Mixture of Gaussians) for the 
%         first house
%   GMM2: sensor profiles for the second house
%   dist: a |GMM1|x|GMM2| matrix of similarities between sensor profiles.
%         dist(a,b) contains the similarity value of the sensor 'a' in
%         the first house compared to the sensor 'b' in the second.
%
% Tip: the parameters for the sensor profiles can be tweaked editing from
%      inside the function 'gm_process_sensor'


% Input
HOUSE1 = houseA;
HOUSE2 = houseB;

% Process sensors' information
fprintf('Formatting the first house...\n');
datas1 = reformat(HOUSE1,0);
fprintf('Formatting the second house...\n');
datas2 = reformat(HOUSE2,0);

% Compute the sensor profiles in the first house
for i=1:length( HOUSE1.sensor_labels ),
    sid = HOUSE1.sensor_labels{ i, 1 };
    vec = datas1(:,5)==sid;
    data = [ datas1(:,1).*vec datas1(:,2).*vec datas1(:,3).*vec datas1(:,4).*vec datas1(:,5).*vec];
    data = data(any(data,2),any(data,1));
    GMM1{i} = gm_process_sensor(data, '', 1);
end

% Compute the sensor profiles in the second house
for i=1:length( HOUSE2.sensor_labels ),
    sid = HOUSE2.sensor_labels{ i, 1 };
    vec = datas2(:,5)==sid;
    data = [ datas2(:,1).*vec datas2(:,2).*vec datas2(:,3).*vec datas2(:,4).*vec datas2(:,5).*vec];
    data = data(any(data,2),any(data,1));
    GMM2{i} = gm_process_sensor(data, '', 1);
end

% Compute the similarities
for i=1:length(GMM1),
    fprintf('Matching sensor %d/%d...\n',i,length(GMM1));
    for j=1:length(GMM2),
            dist(i,j) = gm_compare_jmef(GMM1{i}, GMM2{j});
    end
end
